#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#define Die(code)   \
    do              \
    {               \
        exit(code); \
    } while (0)

#define CONV(v) (struct sockaddr *)(v)
enum
{
    USAGE_ERR = 1,
    SOCKET_ERR,
    BIND_ERR,
    LISTEN_ERR,
    EPOLL_CREATE_ERR,
    EPOLL_CTL_ERR
};
const int gdefaultsockfd = -1;
const int gbacklog = 8;
bool SplitString(std::string &str, const std::string &sep, std::string *key, std::string *value)
{
    auto pos = str.find(sep);
    if (pos == std::string::npos)
    {
        return false;
    }
    *key = str.substr(0, pos);
    *value = str.substr(pos + sep.size());
    return true;
}
bool ParseOneline(std::string &str, std::string *out, const std::string &sep)
{
    auto pos = str.find(sep);
    if (pos == std::string::npos)
    {
        return false;
    }
    *out = str.substr(0, pos);
    str.erase(0, pos + sep.size());

    return true;
}
void SetNonBlock(int sockfd)
{
    int fl = fcntl(sockfd, F_GETFL);
    if (fl < 0)
    {
        return;
    }
    fcntl(sockfd, F_SETFL, fl | O_NONBLOCK);
}